package com.geek.controller;

import com.geek.bean.PageData;
import com.geek.bean.PageParameter;
import com.geek.bean.data.CategoryData;
import com.geek.bean.BaseRespVo;
import com.geek.bean.data.GoodsDetailData;
import com.geek.bean.vo.CategoryVo;
import com.geek.model.MarketGoods;
import com.geek.service.CategoryService;
import com.geek.service.GoodsService;
import com.github.pagehelper.Page;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController
@RequestMapping("wx/goods")
public class WxGoodsController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    CategoryService categoryService;

    // 搜索框中商品数
    @RequestMapping("count")
    public BaseRespVo count() {
        long count = goodsService.count();
        return BaseRespVo.ok(count);
    }

    // 商品类目
    @RequestMapping("category")
    public BaseRespVo category(Integer id) {
        CategoryData date = categoryService.category(id);
        return BaseRespVo.ok(date);
    }

    // 商品显示 搜索历史
    @RequestMapping("list")
    public BaseRespVo listAll(PageParameter pageParameter, Integer categoryId ,String keyword ,Integer brandId) {
        PageData data = goodsService.listHistory(pageParameter,categoryId,keyword,brandId);
        return BaseRespVo.ok(data);
    }

    // 商品详情
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();
        if (!subject.isAuthenticated()) {
            userId = 0;
            GoodsDetailData data = goodsService.detail(id, userId);
            return BaseRespVo.ok(data);
        } else {
            GoodsDetailData data = goodsService.detail(id, userId);
            return BaseRespVo.ok(data);
        }

    }

    // 相关商品
    @RequestMapping("related")
    public BaseRespVo related(Integer id) {
        // 查找当前商品
        MarketGoods marketGoods = goodsService.findById(id);
        // 从当前商品的所有信息中获取类目Id
        Integer categoryId = marketGoods.getCategoryId();

        int page = 1;
        int limit = 6;
        PageData data = goodsService.queryByCategory(categoryId, page, limit);
        return BaseRespVo.ok(data);
    }

}
